package com.yao.handler;

import com.alibaba.fastjson.JSONObject;
import com.yao.bean.R;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Copyright (C), 2022-2022, 姚兵
 * Author: 32210
 * Date: 2022/10/10 13:45
 * FileName: MyAccessDenyHandler
 * Description: 12
 */

@Component
public class MyAccessDenyHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        response.setStatus(200);
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();

        R r = new R();
        r.setCode(401);
        r.setData("权限不足");
        r.setMsg("权限不足");
        out.write(JSONObject.toJSONString(r));
        out.flush();
        out.close();
    }
}
